package com.youshang520i.learningmysqlcanal.service.impl;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.youshang520i.learningmysqlcanal.dao.OperationRecordMapper;
import com.youshang520i.learningmysqlcanal.model.OperationRecord;
import com.youshang520i.learningmysqlcanal.service.OperationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author youshang
 */
@Service
public class OperationRecordServiceImpl implements OperationRecordService {
    @Autowired
    private OperationRecordMapper operationRecordMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return operationRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(OperationRecord record) {
        return operationRecordMapper.insert(record);
    }

    @Override
    public int insertSelective(List<CanalEntry.RowData> rowChange, String tableName) {
        OperationRecord operationRecord = new OperationRecord();
        StringBuffer afterSb = new StringBuffer();
        StringBuffer beforeSb = new StringBuffer();
        StringBuffer columnSb = new StringBuffer();
        StringBuffer keyIdSb = new StringBuffer();
        for (CanalEntry.RowData data : rowChange) {
            Map<Integer,String> map = new LinkedHashMap<>();
            String keyId = "";
            //修改后
            for (CanalEntry.Column column : data.getAfterColumnsList()) {
                if (column.getIsKey()) {
                    keyId = column.getValue();
                }
                if (column.getUpdated()){
                    map.put(column.getIndex(),column.getValue());
                    afterSb.append(column.getValue() + ",");
                    columnSb.append(column.getName() + ",");
                }
            }


            //修改前
            for (CanalEntry.Column column : data.getBeforeColumnsList()) {
                if (column.getIsKey()) {
                    if (keyId.equals(column.getValue())) {
//                        System.out.println(" 主健ID： "+ keyId);
                        for (Map.Entry<Integer, String> integerStringEntry : map.entrySet()) {
                            CanalEntry.Column column1 = data.getBeforeColumnsList().get(integerStringEntry.getKey());
                            String value = column1.getValue();
//                                                System.out.println(" 修改前的值： "+ "  "+ tableName +"    " +name + "      " + "   -----    "+ value);
//                                                System.out.println(" 修改后的值： "+ "  "+ tableName +"    " +name + "      " + "   -----    "+ integerStringEntry.getValue());
                            beforeSb.append(value + ",");
                        }
                    }
                }
            }
            keyIdSb.append(keyId + ",");
        }

        operationRecord.setTableName(tableName);
        operationRecord.setColumnKeyValue(keyIdSb.toString());
        operationRecord.setCreateTime(new Date());
        operationRecord.setColumnName(columnSb.toString());
        operationRecord.setBeforeValue(beforeSb.toString());
        operationRecord.setAfterValue(afterSb.toString());
        operationRecord.setOperationType(1);
        return operationRecordMapper.insertSelective(operationRecord);
    }

    @Override
    public OperationRecord selectByPrimaryKey(Integer id) {
        return operationRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(OperationRecord record) {
        return operationRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(OperationRecord record) {
        return operationRecordMapper.updateByPrimaryKey(record);
    }
}
